package cn.edu.njuit.springbootfilmreview.service.impl;

import cn.edu.njuit.springbootfilmreview.dto.AccountInfo;
import cn.edu.njuit.springbootfilmreview.dto.MovieInfo;
import cn.edu.njuit.springbootfilmreview.service.AccountInfoService;
import cn.edu.njuit.springbootfilmreview.mapper.AccountInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【account_info(登录用户表)】的数据库操作Service实现
 * @createDate 2022-05-23 19:35:25
 */
@Service
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo>
        implements AccountInfoService {


    @Autowired
    AccountInfoMapper accountInfoMapper;

    @Override
    public AccountInfo getAccountByUserName(String username) {
        return accountInfoMapper.selectOne(new LambdaQueryWrapper<AccountInfo>()
                .eq(AccountInfo::getUsername, username));
    }

    @Override
    public List<AccountInfo> getAccountSByUserName(String username) {
        return accountInfoMapper.selectList(new LambdaQueryWrapper<AccountInfo>()
                .eq(AccountInfo::getUsername, username));
    }

    @Override
    public List<AccountInfo> getAllAccount() {
        return accountInfoMapper.selectList(null);
    }

    @Override
    public boolean isLogin(AccountInfo accountInfo) {
        AccountInfo accountInfo1 = accountInfoMapper.getUserByLoginAndPassword(accountInfo.getUsername(), accountInfo.getPassword());
        if (accountInfo1.getStatus() == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean loginValidate(AccountInfo accountInfo) {
        AccountInfo accountInfo1 = accountInfoMapper.getUserByLoginAndPassword(accountInfo.getUsername(), accountInfo.getPassword());
        return accountInfo1 != null;
    }

    @Override
    public AccountInfo insertAccountInfo(AccountInfo accountInfo) {
        int insert = accountInfoMapper.insert(accountInfo);
        AccountInfo accountInfo1 = accountInfoMapper.selectOne(new LambdaQueryWrapper<AccountInfo>()
                .eq(AccountInfo::getUsername, accountInfo.getUsername()));
        if (insert > 0) {
            return accountInfo1;
        } else {
            return null;
        }
    }

    @Override
    public boolean updateAccountInfoStatusISTure(Long id) {
        LambdaUpdateWrapper<AccountInfo> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AccountInfo::getStatus, 0);
        lqw.eq(AccountInfo::getId, id);
        return this.update(lqw);
    }

    @Override
    public boolean updateAccountInfoStatusISFalse(Long id) {
        LambdaUpdateWrapper<AccountInfo> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AccountInfo::getStatus, 1);
        lqw.eq(AccountInfo::getId, id);
        return this.update(lqw);
    }

    @Override
    public AccountInfo getAccountById(Long id) {
        return accountInfoMapper.selectOne(new LambdaQueryWrapper<AccountInfo>()
                .eq(AccountInfo::getId, id));
    }

}
